//
//  GWRaidersMainViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/9.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWRaidersMainViewController.h"
#import "GWCollectionsModel.h"
#import "GWChannelsModel.h"
#import "GWChannelsGroupModel.h"
#import "UIImageView+WebCache.h"
#import "GWCollectionsCell.h"
#import "GWChannelsCell.h"

@interface GWRaidersMainViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *collectionsArr;
@property (nonatomic, strong) NSMutableArray *channelGroupArr;

@end

@implementation GWRaidersMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.backgroundColor = [UIColor colorWithWhite:0.913 alpha:1.000];
    [_mainTableView registerClass:[GWCollectionsCell class] forCellReuseIdentifier:@"collectionsCell"];
    [_mainTableView registerClass:[GWChannelsCell class] forCellReuseIdentifier:@"channelsCell"];
    [self.view addSubview:_mainTableView];
    [self requestData];
}

- (NSMutableArray *)collectionsArr {
    if (!_collectionsArr) {
        _collectionsArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _collectionsArr;
}

- (NSMutableArray *)channelGroupArr {
    if (!_channelGroupArr) {
        _channelGroupArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _channelGroupArr;
}

- (void)requestData {
    
    AFHTTPRequestOperation *opreation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/collections?limit=6&offset=0"]]];
    [opreation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.collectionsArr removeAllObjects];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *collectionsDic in dict[@"data"][@"collections"]) {
            GWCollectionsModel *model = [[GWCollectionsModel alloc] init];
            [model setValuesForKeysWithDictionary:collectionsDic];
            [self.collectionsArr addObject:model];
        }
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [opreation start];
    
    AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/channel_groups/all"]]];
    [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        [self.channelGroupArr removeAllObjects];
        for (NSDictionary *modelDic in dict[@"data"][@"channel_groups"]) {
            GWChannelsGroupModel *model = [[GWChannelsGroupModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.channelGroupArr addObject:model];
        }
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation1 start];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat x = (SCREEN_WIDTH - 50)/4;
    switch (indexPath.section) {
        case 0:
            return 120;
            break;
        case 1:
            return 100+5*x;
            break;
        case 2:
            return 100+2*x;
            break;
        case 3:
            return 100+2*x;
            break;
            
        default:
            return 100+2*x;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        GWCollectionsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectionsCell" forIndexPath:indexPath];
        cell.collectionsArr = self.collectionsArr;
        cell.naviC = self.naviC;
        [cell.collectionView reloadData];
        return cell;
    } else {
        GWChannelsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"channelsCell" forIndexPath:indexPath];
        GWChannelsGroupModel *model = [[GWChannelsGroupModel alloc] init];
        model = _channelGroupArr[indexPath.section - 1];
        cell.title = model.name;
        cell.channelsArr = model.channels;
        cell.naviC = self.naviC;
        [cell set];
        [cell.collectionView reloadData];
        return cell;
    }
}

//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    if (collectionView.tag == 100) {
//        return self.collectionsArr.count;
//    } else {
//        return 0;
//    }
//}
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    
//    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ztCell" forIndexPath:indexPath];
//    GWCollectionsModel *model = [[GWCollectionsModel alloc] init];
//    model = _collectionsArr[indexPath.item];
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 130, 60)];
//    imageView.layer.cornerRadius = 5;
//    imageView.layer.masksToBounds = YES;
//    [imageView sd_setImageWithURL:[NSURL URLWithString:model.banner_image_url]];
//    [cell addSubview:imageView];
//    return cell;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
